package com.huawei.hms.framework.network.restclient.hwhttp.route;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.network.restclient.Headers;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo.MetricsTime;
import com.huawei.hms.framework.network.restclient.hwhttp.HttpClient;
import com.huawei.hms.framework.network.restclient.hwhttp.Interceptor;
import com.huawei.hms.framework.network.restclient.hwhttp.Interceptor.Chain;
import com.huawei.hms.framework.network.restclient.hwhttp.RealInterceptorChain;
import com.huawei.hms.framework.network.restclient.hwhttp.Request;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestTask;
import com.huawei.hms.framework.network.restclient.hwhttp.Response;
import com.huawei.hms.framework.network.restclient.hwhttp.cronet.CronetNegotiateManager;
import com.huawei.hms.framework.network.restclient.hwhttp.cronet.CronetRequestTask;
import com.huawei.hms.framework.network.restclient.hwhttp.url.HttpUrl;
import java.io.IOException;

public class RouteInterceptor implements Interceptor {
    private static final String ALT_SVC = "alt-svc";
    private static final String EQUAL = "=";
    private static final String QUIC = "quic";
    private static final String SEMICOLON = ";";
    private static final String TAG = "RouteInterceptor";

    public Response intercept(Chain chain) throws IOException {
        Exception e;
        int i;
        Throwable th;
        MetricsTime metricsRealTime;
        long secureConnectStartTime;
        Headers headers = null;
        RequestTask requestTask = ((RealInterceptorChain) chain).getRequestTask();
        Request connectionDynamicDelay = HostRoute.getInstance().setConnectionDynamicDelay(chain.request());
        HttpUrl url = connectionDynamicDelay.getUrl();
        String host = url.getHost();
        int port = url.getPort();
        RequestFinishedInfo requestFinishedInfo;
        try {
            HostRoute.getInstance().processDomains(host);
            Response proceed = chain.proceed(connectionDynamicDelay);
            if (requestTask != null) {
                requestFinishedInfo = requestTask.getRequestFinishedInfo();
                if (requestFinishedInfo != null) {
                    try {
                        Logger.v(TAG, "the net lib is %s", requestFinishedInfo.getNetworkSdkType());
                        Logger.v(TAG, "the protocol is %s", requestFinishedInfo.getMetrics().getProtocol());
                    } catch (Exception e2) {
                        e = e2;
                        i = port;
                        try {
                            if (requestTask instanceof CronetRequestTask) {
                                CronetNegotiateManager.getInstance().updateQuicHints(host, i, false);
                            }
                            throw e;
                        } catch (Throwable th2) {
                            th = th2;
                            if (requestFinishedInfo != null) {
                                metricsRealTime = requestFinishedInfo.getMetricsRealTime();
                                secureConnectStartTime = metricsRealTime.getSecureConnectStartTime() - metricsRealTime.getConnectStartTime();
                                if (secureConnectStartTime <= 0) {
                                    secureConnectStartTime = metricsRealTime.getConnectEndTime() - metricsRealTime.getConnectStartTime();
                                }
                                HostRoute.getInstance().updateIntervalMap(secureConnectStartTime, host);
                            }
                            throw th;
                        }
                    }
                }
            }
            requestFinishedInfo = null;
            if (proceed != null) {
                headers = proceed.getHeaders();
            }
            HttpClient client = ((RealInterceptorChain) chain).getClient();
            if (!(headers == null || client == null || !client.quicEnabled())) {
                String str = headers.get(ALT_SVC);
                if (str != null && str.contains(QUIC)) {
                    i = getValueFromHeader(str, QUIC);
                    try {
                        Logger.v(TAG, "the port is %s", Integer.valueOf(i));
                        CronetNegotiateManager.getInstance().updateQuicHints(host, i, true);
                    } catch (Exception e3) {
                        e = e3;
                        if (requestTask instanceof CronetRequestTask) {
                            CronetNegotiateManager.getInstance().updateQuicHints(host, i, false);
                        }
                        throw e;
                    }
                }
            }
            if (requestFinishedInfo != null) {
                MetricsTime metricsRealTime2 = requestFinishedInfo.getMetricsRealTime();
                secureConnectStartTime = metricsRealTime2.getSecureConnectStartTime() - metricsRealTime2.getConnectStartTime();
                if (secureConnectStartTime <= 0) {
                    secureConnectStartTime = metricsRealTime2.getConnectEndTime() - metricsRealTime2.getConnectStartTime();
                }
                HostRoute.getInstance().updateIntervalMap(secureConnectStartTime, host);
            }
            return proceed;
        } catch (Exception e4) {
            e = e4;
            requestFinishedInfo = null;
            i = port;
            if (requestTask instanceof CronetRequestTask) {
                CronetNegotiateManager.getInstance().updateQuicHints(host, i, false);
            }
            throw e;
        } catch (Throwable th22) {
            requestFinishedInfo = null;
            th = th22;
            if (requestFinishedInfo != null) {
                metricsRealTime = requestFinishedInfo.getMetricsRealTime();
                secureConnectStartTime = metricsRealTime.getSecureConnectStartTime() - metricsRealTime.getConnectStartTime();
                if (secureConnectStartTime <= 0) {
                    secureConnectStartTime = metricsRealTime.getConnectEndTime() - metricsRealTime.getConnectStartTime();
                }
                HostRoute.getInstance().updateIntervalMap(secureConnectStartTime, host);
            }
            throw th;
        }
    }

    private int getValueFromHeader(String str, String str2) {
        Logger.v(TAG, "the quicFiled is %s-%s", str, str2);
        if (str == null || str2.isEmpty()) {
            return 0;
        }
        String[] split = str.split(SEMICOLON);
        int i = 0;
        for (String split2 : split) {
            String split22;
            String[] split3 = split22.split("=");
            if (split3.length == 2 && str2.equals(split3[0].trim())) {
                split22 = split3[1].trim().replace("\"", "").replace(":", "");
                if (split22.length() > 0) {
                    Logger.v(TAG, "the temport is %s", split22);
                    try {
                        i = Integer.valueOf(split22).intValue();
                    } catch (Throwable e) {
                        Logger.w(TAG, "the port convert has error", e);
                    }
                }
            }
        }
        return i;
    }
}
